package main

import (
    "encoding/json"
    "log"
    "os"
    "strconv"

    "github.com/xuri/excelize/v2"
)

type Property struct {
    Name        string      `json:"name"`
    Label       string      `json:"label"`
    AccessMode  int         `json:"accessMode"`
    Type        int         `json:"type"`
    Params      Params      `json:"params"`
    Decimals    int         `json:"decimals"`
    Unit        string      `json:"unit"`
    Formula     string      `json:"formula"`
    WFormula    string      `json:"wFormula"`
    Script      string      `json:"script"`
    Value       interface{} `json:"value"`
}

type Params struct {
    DbNumber  string `json:"dbNumber"`
    DataType  int    `json:"dataType"`
    StartAddr string `json:"startAddr"`
}

type Device struct {
    Index      int        `json:"index"`
    Name       string     `json:"name"`
    Label      string     `json:"label"`
    Type       int        `json:"type"`
    Properties []Property `json:"properties"`
}

func main() {
    // 打开JSON文件
    file, err := os.Open("TSLModelS7Param.json")
    if err != nil {
        log.Fatalf("无法打开文件: %v", err)
    }
    defer file.Close()

    // 读取JSON文件内容
    decoder := json.NewDecoder(file)
    var devices map[string]Device
    if err := decoder.Decode(&devices); err != nil {
        log.Fatalf("无法解析JSON: %v", err)
    }

    // 创建一个新的Excel文件
    f := excelize.NewFile()

    // 遍历设备并将它们分别存储在不同的Sheet中
    for key, device := range devices {
        sheetName := key
        f.NewSheet(sheetName)

        // 定义表头
        headers := []string{"Key", "Name", "Label", "Type", "Property Name", "Property Label", "Access Mode", "Property Type", 
                            "DB Number", "Data Type", "Start Address", "Decimals", "Unit", "Formula", "WFormula", "Script", "Value"}
        for col, header := range headers {
            cell, _ := excelize.CoordinatesToCellName(col+1, 1)
            f.SetCellValue(sheetName, cell, header)
        }

        // 添加设备数据
        for row, prop := range device.Properties {
            f.SetCellValue(sheetName, "A"+strconv.Itoa(row+2), key)
            f.SetCellValue(sheetName, "B"+strconv.Itoa(row+2), device.Name)
            f.SetCellValue(sheetName, "C"+strconv.Itoa(row+2), device.Label)
            f.SetCellValue(sheetName, "D"+strconv.Itoa(row+2), device.Type)
            f.SetCellValue(sheetName, "E"+strconv.Itoa(row+2), prop.Name)
            f.SetCellValue(sheetName, "F"+strconv.Itoa(row+2), prop.Label)
            f.SetCellValue(sheetName, "G"+strconv.Itoa(row+2), prop.AccessMode)
            f.SetCellValue(sheetName, "H"+strconv.Itoa(row+2), prop.Type)
            f.SetCellValue(sheetName, "I"+strconv.Itoa(row+2), prop.Params.DbNumber)
            f.SetCellValue(sheetName, "J"+strconv.Itoa(row+2), prop.Params.DataType)
            f.SetCellValue(sheetName, "K"+strconv.Itoa(row+2), prop.Params.StartAddr)
            f.SetCellValue(sheetName, "L"+strconv.Itoa(row+2), prop.Decimals)
            f.SetCellValue(sheetName, "M"+strconv.Itoa(row+2), prop.Unit)
            f.SetCellValue(sheetName, "N"+strconv.Itoa(row+2), prop.Formula)
            f.SetCellValue(sheetName, "O"+strconv.Itoa(row+2), prop.WFormula)
            f.SetCellValue(sheetName, "P"+strconv.Itoa(row+2), prop.Script)
            f.SetCellValue(sheetName, "Q"+strconv.Itoa(row+2), prop.Value)
        }
    }

    // 删除默认的Sheet
    f.DeleteSheet("Sheet1")

    // 保存文件
    if err := f.SaveAs("output.xlsx"); err != nil {
        log.Fatalf("无法保存文件: %v", err)
    }
}
